package reesoft.idgenerator.service.idstore;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import reesoft.common.util.ApplicationContextUtil;

/**
 * 序号存储工厂类
 *
 * Created by reesoft on 2017-08-28
 */
@ConfigurationProperties(prefix = "id.store")
@Component
public class IdStoreFactory
{
    private static final Logger LOG = LoggerFactory.getLogger(IdStoreFactory.class);

    @Autowired
    private ApplicationContextUtil applicationContextUtil;

    /**
     * 序号存储类名
     */
    private String className;

    public String getClassName()
    {
        return className;
    }

    public void setClassName(String className)
    {
        this.className = className;
    }

    /**
     * 构造序号存储对象
     */
    public IdStore getIdStore()
    {
        try
        {
            Class clazz = Class.forName(className);
            Object object = clazz.newInstance();
            return (IdStore) object;
        }
        catch (Exception e)
        {
            LOG.error(e.toString());
        }

        return null;
    }
}
